package com.toommi.refuse.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toommi.refuse.entity.Motorcade;
import com.toommi.refuse.entity.MotorcadeExample;
import com.toommi.refuse.enums.AdminExceptionEnum;
import com.toommi.refuse.exception.AdminException;
import com.toommi.refuse.mapper.MotorcadeMapper;
import com.toommi.refuse.service.MotorcadeService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yangg
 * @since 2019-07-31 16:36
 */
@Service
@AllArgsConstructor
public class MotorcadeServiceImpl implements MotorcadeService {

    private MotorcadeMapper motorcadeMapper;


    @Override
    public void addMotorcade(Motorcade motorcade) throws AdminException {

        if (StringUtils.isEmpty(motorcade.getName())) {
            throw new AdminException(AdminExceptionEnum.INVALID_MOTORCADE_NAME_NULL);
        }

        motorcade.setId(0L);

        motorcade.setCreateTime(System.currentTimeMillis());
        motorcade.setUpdateTime(System.currentTimeMillis());

        motorcadeMapper.insert(motorcade);

    }

    @Override
    public void deleteMotorcade(long id) {
        motorcadeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Motorcade> findMotorcades(int pageNum, int pageSize, String keyWord) {

        PageHelper.startPage(pageNum, pageSize);

        MotorcadeExample example = new MotorcadeExample();
        example.createCriteria().andNameLike("%"+keyWord+"%");
        List<Motorcade> motorcades = motorcadeMapper.selectByExample(example);

        return new PageInfo<>(motorcades);
    }

    @Override
    public Motorcade findMotorcade(long id) {
        return motorcadeMapper.selectByPrimaryKey(id);
    }
}
